package com.huiquan.foundation.controller;

import com.huiquan.foundation.service.SubpackageService;
import com.huiquan.foundation.util.RequestUtils;
import com.huiquan.framework.base.ReturnCode;
import com.huiquan.framework.utils.ReturnUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lichao email:lichao@witspring.com
 * @Description: 分包controller
 * @Date 2017/11/21 10:06
 * @since Ver V
 */
@Controller
public class SubpackageController {
    protected final Logger LOGGER = LoggerFactory.getLogger(getClass());

    private static final String appkey = "d905d8fb-d5b4-4f56-ba51-8fa875428191";

    @Autowired
    private SubpackageService subpackageService;

    /**
     * 分配包给用户
     *
     * @param req
     * @param resp
     */
    @RequestMapping("/subpackage/subpackageToUser.do")
    public void subpackageToUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        if (!appkey.equals(req.getParameter("appkey"))) {
            RequestUtils.constructResponse(req, resp, ReturnUtil.fail(ReturnCode.APPKEY_ERROR));
            return;
        }
        int type;
        long uid;
        String param;
        String level;
        try {
            type = Integer.parseInt(req.getParameter("type"));
            uid = Long.parseLong(req.getParameter("uid"));
            param = req.getParameter("param");
            level = req.getParameter("level");
        } catch (NumberFormatException e) {
            LOGGER.error("输入参数异常");
            RequestUtils.constructResponse(req, resp, ReturnUtil.fail(ReturnCode.PARAM));
            return;
        }
        Map<String, Object> data = new HashMap<>();
        String idPackage = subpackageService.subpackageToUser(type, level, param, uid);
        data.put("idPackage", idPackage);
        RequestUtils.constructResponse(req, resp, ReturnUtil.success(data));
    }

    /**
     * 获取sidList给外包人员
     *
     * @param req
     * @param resp
     */
    @Deprecated
    @ResponseBody
    @RequestMapping("/subpackage/getSidListToUser.do")
    public void getSidListToUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        if (!appkey.equals(req.getParameter("appkey"))) {
            RequestUtils.constructResponse(req, resp, ReturnUtil.fail(ReturnCode.APPKEY_ERROR));
            return;
        }
        String secondType = req.getParameter("secondType");
        long uid;
        try {
            if (StringUtils.isEmpty(secondType)) {
                throw new Exception();
            }
            uid = Long.parseLong(req.getParameter("uid"));
        } catch (Exception e) {
            LOGGER.error("输入参数异常");
            RequestUtils.constructResponse(req, resp, ReturnUtil.fail(ReturnCode.PARAM));
            return;
        }
        List<Long> sidList = subpackageService.getSidListToUser(secondType, uid);
        Map<String, Object> data = new HashMap<>();
        data.put("sidList", sidList);
        RequestUtils.constructResponse(req, resp, ReturnUtil.success(data));
    }

    /**
     * 校验用户提交的数据
     *
     * @param req
     * @param resp
     * @throws IOException
     */
    @Deprecated
    @ResponseBody
    @RequestMapping("/subpackage/checkUserCommit.do")
    public void checkUserCommit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        if (!appkey.equals(req.getParameter("appkey"))) {
            RequestUtils.constructResponse(req, resp, ReturnUtil.fail(ReturnCode.APPKEY_ERROR));
            return;
        }
        long sid;
        try {
            sid = Long.parseLong(req.getParameter("sid"));
        } catch (Exception e) {
            LOGGER.error("输入参数异常");
            RequestUtils.constructResponse(req, resp, ReturnUtil.fail(ReturnCode.PARAM));
            return;
        }
        subpackageService.checkUserCommit(sid);
        RequestUtils.constructResponse(req, resp, ReturnUtil.success());
    }

}
